import * as request from './request'

interface BaseRes {
  stat: string
  msg?: string
  data?: any
}

//注册
export function registry(account: string, pwd: string) {
  return request.post<BaseRes>(`/api/auth/registry`, {
    account: account,
    pwd: pwd,
  })
}

//登录
export function login(account: string, pwd: string) {
  return request.post<BaseRes>(`/api/auth/login`, {
    account: account,
    pwd: pwd,
  })
}

//登出
export function logout(token: string) {
  return request.post<BaseRes>(
    `/api/auth/logout`,
    {},
      null
    ,
  )
}

//更新用户信息
export function updateUserInfo(
  token: string,
  avatar: string,
  nickname: string,
) {
  return request.put<BaseRes>(
    `/api/user/updateUserInfo`,
    {
      avatar: avatar,
      nickname: nickname,
    },
    null
  )
}

//更新用户密码
export function updatePwd(token: string, oldPwd: string, newPwd: string) {
  return request.put<BaseRes>(
    `/api/user/updatePwd`,
    {
      oldPwd: oldPwd,
      newPwd: newPwd,
    },
    null
  )
}

//得到用户信息
export function getUserInfo(token: string) {
  return request.get<BaseRes>(`/api/user/userInfo`, null)
}

//搜索该用户所有的文章
export function searchOwnerArticle(
  token: string,
  pageIndex: number,
  pageSize: number,
  keyword: string,
) {
  return request.post<BaseRes>(
    `/api/article/searchOwner`,
    {
      pageIndex: pageIndex,
      pageSize: pageSize,
      keyword: keyword,
    },
    null
  )
}

//搜索所有的文章
export function searchAllArticle(
  pageIndex: number,
  pageSize: number,
  keyword: string,
) {
  return request.post<BaseRes>(`/api/article/searchAll`, {
    pageIndex: pageIndex,
    pageSize: pageSize,
    keyword: keyword,
  })
}

//添加文章
export function addArticle(
  token: string,
  content: string,
  title: string,
  subTitle: string,
  tags: string[],
  banner: string[],
) {
  return request.post<BaseRes>(
    `/api/article/add`,
    {
      content: content,
      title: title,
      subTitle: subTitle,
      tags: tags,
      banner: banner,
    },
    null
  )
}

//删除文章
export function deleteArticle(token: string, articleId: string) {
  return request.deleteApi<BaseRes>(`/api/article/${articleId}`, null)
}

//更新文章内容
export function setArticle(
  token: string,
  articleId: string,
  content: string,
  title: string,
  subTitle: string,
  tags: string[],
) {
  return request.put<BaseRes>(
    `/api/article/${articleId}`,
    {
      content: content,
      title: title,
      subTitle: subTitle,
      tags: tags,
    },
    null
  )
}

//增加文章浏览量
export function getArticle(token: string, articleId: string) {
  return request.get<BaseRes>(`/api/article/${articleId}`, null)
}

//上传图片
export function uploadFile(token: string, file: FormData) {
  return request.post<BaseRes>(`/api/file/upload`, file, null)
}

//下载图片
export function downloadFile(key: string) {
  return request.get<BaseRes>(`/api/file/download/${key}`, {
    responseType: 'blob',
  })
}

//得到图片
export function previewFile(key: string) {
  return request.get<BaseRes>(`/api/file/preview/${key}`, {
    responseType: 'blob',
  })
}
